package com.zy.algorithm.sort.quick;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.zy.algorithm.R;
import com.zy.algorithm.controller.IndexController;
import com.zy.algorithm.controller.SubSetController;
import com.zy.algorithm.controller.screen.BaseBallFirstScreenController;
import com.zy.algorithm.controller.screen.QuickChildScreenController;
import com.zy.algorithm.controller.screen.QuickFirstScreenController;
import com.zy.algorithm.curtain.IScreenBean;
import com.zy.algorithm.curtain.QuickScreenBean;
import com.zy.algorithm.fragment.BaseAlgorithmBallFragment;
import com.zy.view.SubSetTipsView;

import java.util.ArrayList;
import java.util.List;

public abstract class QuickSortBaseFragment extends BaseAlgorithmBallFragment {

    private static final String TAG = "QuickSortBaseFragment";

    private SubSetController subSetController;

    FrameLayout child_screen_ui_container;

    @Override
    protected boolean getColorAni() {
        return false;
    }

    @Override
    protected String getDataDes() {
        return "快速排序";
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        firstScreenController.setPKShow(false, false, true);

        child_screen_ui_container = view.findViewById(R.id.child_screen_ui_container);

        indexController.hideAllIndex(false, true);
        sort_index_tips_container.setVisibility(View.GONE);

        subSetController = new SubSetController(view);
        subSetController.show();
    }

    @Override
    protected BaseBallFirstScreenController buildScreenController(View view, boolean colorAni, IndexController indexController) {
        return new QuickFirstScreenController(view, colorAni, indexController);
    }

    @Override
    protected void sortAnimation(int index, IScreenBean screenBean, StepListener listener) {
        //super.sortAnimation(index, screenBean, listener);

        if (!(screenBean instanceof QuickScreenBean)) {
            return;
        }
        QuickScreenBean bean = (QuickScreenBean) screenBean;

        List<Integer> subSetIndexList = bean.getSubSetIndexList();

        Log.d(TAG, "" + bean);

        QuickChildScreenController quickChildScreenController = null;
        if (screenBean.isNeedScreenAnimation()) {
            quickChildScreenController = new QuickChildScreenController(child_screen_ui_container);
            quickChildScreenController.setScreenBean(bean);
            indexController.hideAllIndex();
        }

        QuickChildScreenController finalQuickChildScreenController = quickChildScreenController;
        //分组动画
        subSetController.showSubSet(toSubSetNode(subSetIndexList), new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (finalQuickChildScreenController != null) {
                    //页面进入动画
                    finalQuickChildScreenController.startPageEnterAnimator(firstScreenController,
                            bean,
                            firstScreenController.getSubSetIndexListView(subSetIndexList),
                            listener
                    );
                } else {
                    //
                    firstScreenController.startSubCartoon(bean.getCartoonStepBeanList(), listener);
                }
            }
        });
    }


    private List<SubSetTipsView.SubSetNode> toSubSetNode(List<Integer> subSetIndexList) {
        List<SubSetTipsView.SubSetNode> nodes = new ArrayList<>();


        int heightBase = 100;
        int[] BASE_COLORS = new int[]{Color.RED, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.CYAN, Color.RED, Color.RED};
        List<Integer> xPointList = new ArrayList<>();


        //下标转换成-px
        for (Integer index : subSetIndexList) {
            xPointList.add(getCenterXByIndex(index) - 30);
        }

        SubSetTipsView.SubSetNode node = new SubSetTipsView.SubSetNode(xPointList, heightBase, BASE_COLORS[0]);
        nodes.add(node);

        return nodes;
    }
}
